package br.com.materiais.dao;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.materiais.factory.MaterialConnectionFactory;
import br.com.materiais.model.LoginModel;



public class LoginDao 
{
	private Session session = null;
	Transaction tx = null;
	public LoginDao()
	{
		
	}
	
	
	@SuppressWarnings("unchecked")
	public String getUsuario(String usuario,String senha)
	{
		String SQLCONSULTA = "from LoginModel as l WHERE l.usuario=:usuario AND l.senha=:senha";
		Session session = MaterialConnectionFactory.getInstance( );
		
		Query query = session.createQuery( SQLCONSULTA );
		
		List login =  query.setParameter("usuario", usuario).setParameter("senha", senha).list();
		
		if(!login.isEmpty())
		    return "ok";
		
		return "";
	}
	
	public void salvarUsuario(LoginModel pLogin)
	{   
		 session = MaterialConnectionFactory.getInstance( );
		   try
		   {
			   tx = session.beginTransaction();
			   session.save(pLogin);
			   tx.commit();
		   }
		   
		   catch(HibernateException hex)
		   {
			   hex.printStackTrace();
			   tx.rollback();
		   }
		   
		   finally
		   {
			   session.close();
		   }
	}

}
